package com.shell.customer.accreditation.rx;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.accreditation.vo.UserVo;
import com.shell.customer.po.CusRec;
import com.shell.dto.UserAndOrganDto;
import com.shell.feign.PubDictionaryFeign;
import com.shell.feign.PubRoleFeign;
import com.shell.feign.UserOrganFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.authorization.po.PubRole;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.user.vo.PubUserVo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SaveUsersSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private PubRoleFeign pubRoleFeign;
	@Autowired
	private PubDictionaryFeign pubDictionaryFeign;
	@Autowired
	private UserOrganFeign userOrganFeign;

	public void onApplicationEvent(AccreditEvent event) {
		AccreditationVo accreditationVo = event.getAccreditationVo();
		CusRec cus = accreditationVo.getCustomerC();
		List<PubUserVo> pubUserVos = new ArrayList();
		List<PubRole> comUserRoles = this.pubRoleFeign.selectRolesByCodes("COM_USER");

		UserVo administrator = accreditationVo.getAdministrator();
		List<PubRole> adminRoles = this.pubRoleFeign.selectUserRole(administrator.getUserId(), cus.getCusId());
		if ((null != adminRoles) && (adminRoles.size() > 0)) {
			administrator.setRoles(adminRoles);
		} else {
			PubDictionary dict = this.pubDictionaryFeign.getDictionaryByCode(cus.getCusType());
			List<PubRole> roles = this.pubRoleFeign.getRoleByDict(dict);

			administrator.setRoles(roles);
		}
		administrator.setEntId(cus.getCusId());
		administrator.setOrganId(cus.getCusId());
		PubUserVo admin = new PubUserVo();
		BeanUtils.copyProperties(administrator, admin);
		pubUserVos.add(admin);

		List<UserVo> operators = accreditationVo.getOperators();
		String currentTime = DateUtil.getSystemDateTime();
		for (UserVo operator : operators) {
			PubUserVo userVo = new PubUserVo();
			if (StringUtil.empty(operator.getUserId())) {
				operator.setUserId(SystemUtil.getUUID());
				operator.setUserState("I");
				operator.setCreatorId(cus.getContactorId());
				operator.setCreateTime(currentTime);
				operator.setEditorId(cus.getContactorId());
				operator.setEditTime(currentTime);
				operator.setRoles(comUserRoles);
				operator.setEntId(cus.getCusId());
				operator.setOrganId(cus.getCusId());
				BeanUtils.copyProperties(operator, userVo);
				userVo.setUserOrganValid("0");
				pubUserVos.add(userVo);
			} else {
				if (operator.getUserId().equals(administrator.getUserId())) {
					administrator.getRoles().addAll(comUserRoles);
				} else {
					operator.setEntId(cus.getCusId());
					operator.setOrganId(cus.getCusId());
					operator.setUserState("I");
					operator.setRoles(comUserRoles);
					BeanUtils.copyProperties(operator, userVo);
					userVo.setUserOrganValid("0");
					pubUserVos.add(userVo);
				}
				operator.setEditorId(CommonDataUtil.getUserId());
			}
		}
		UserAndOrganDto dto = new UserAndOrganDto();
		dto.setUsers(pubUserVos);
		try {
			this.userOrganFeign.saveOrUpdateUserAndOrgan(JsonUtil.toJson(dto));
		} catch (Exception e) {
			e.printStackTrace();
			throw new UnCheckMsgException("������������������������");
		}
	}
}
